package com.gelin.lease.web.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gelin.lease.common.constant.RedisConstant;
import com.gelin.lease.common.login.LoginUserHolder;
import com.gelin.lease.model.entity.*;
import com.gelin.lease.model.enums.ItemType;
import com.gelin.lease.web.app.mapper.*;
import com.gelin.lease.web.app.mapper.RoomInfoMapper;
import com.gelin.lease.web.app.service.ApartmentInfoService;
import com.gelin.lease.web.app.service.BrowsingHistoryService;
import com.gelin.lease.web.app.service.RoomInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gelin.lease.web.app.vo.apartment.ApartmentItemVo;
import com.gelin.lease.web.app.vo.attr.AttrValueVo;
import com.gelin.lease.web.app.vo.fee.FeeValueVo;
import com.gelin.lease.web.app.vo.graph.GraphVo;
import com.gelin.lease.web.app.vo.room.RoomDetailVo;
import com.gelin.lease.web.app.vo.room.RoomItemVo;
import com.gelin.lease.web.app.vo.room.RoomQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;//房间manpepr
    @Autowired
    private ApartmentInfoService apartmentInfoService;//公寓service
    @Autowired
    private GraphInfoMapper graphInfoMapper;//图片mapper
    @Autowired
    private LeaseTermMapper leaseTermMapper;//租期mapper
    @Autowired
    private AttrValueMapper attrValueMapper;//属性mapper
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;//配套mapper
    @Autowired
    private LabelInfoMapper labelInfoMapper;//标签mapper
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;//支付方式mapper
    @Autowired
    private FeeValueMapper feeValueMapper;//杂费mapper
    @Autowired
    private BrowsingHistoryService browsingHistoryService;//浏览历史service
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;//redis

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //先查redis缓存
        String key = RedisConstant.APP_ROOM_PREFIX + id;
        RoomDetailVo roomDetailVo = (RoomDetailVo) redisTemplate.opsForValue().get(key);
        if (roomDetailVo == null) {
            //1.查询房间信息
            RoomInfo roomInfo = roomInfoMapper.selectById(id);
            if (roomInfo == null) {
                return null;
            }
            //2.获取所属公寓信息
            ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());
            //3.获取图片信息
            List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
            //4.获取属性信息
            List<AttrValueVo> attrValueVoList = attrValueMapper.getDetailByRoomId(id);
            //5.获取配套信息
            List<FacilityInfo> facilityInfoList = facilityInfoMapper.getDetailByRoomId(id);
            //6.获取标签信息
            List<LabelInfo> labelInfoList = labelInfoMapper.getDetailByRoomId(id, ItemType.ROOM);
            //7.获取支付方式信息
            List<PaymentType> paymentTypeList = paymentTypeMapper.getDetailByRoomId(id);
            //8.获取杂费信息
            List<FeeValueVo> feeValueVoList = feeValueMapper.getDetailByRoomId(id);
            //9.获取租期列表信息
            List<LeaseTerm> leaseTerms = leaseTermMapper.getDetailByRoomId(id);
            roomDetailVo = new RoomDetailVo();
            BeanUtils.copyProperties(roomInfo, roomDetailVo);
            roomDetailVo.setApartmentItemVo(apartmentItemVo);
            roomDetailVo.setGraphVoList(graphVoList);
            roomDetailVo.setAttrValueVoList(attrValueVoList);
            roomDetailVo.setFacilityInfoList(facilityInfoList);
            roomDetailVo.setLabelInfoList(labelInfoList);
            roomDetailVo.setPaymentTypeList(paymentTypeList);
            roomDetailVo.setFeeValueVoList(feeValueVoList);
            roomDetailVo.setLeaseTermList(leaseTerms);
            redisTemplate.opsForValue().set(key,roomDetailVo);
        }


        //保存浏览历史
        //先获取用户id
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        browsingHistoryService.saveHistory(userId, id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page, id);
    }
}




